package org.hako.recite.word.tool;

import org.hako.recite.word.parser.WordList;
import org.hako.recite.word.parser.WordListParseException;
import org.hako.recite.word.parser.WordListParser;
import org.hako.recite.word.parser.factory.WordListParserFactory;
import org.hako.recite.word.parser.impl.UnsupportedFileExtensionException;

/**
 * Analyze kannji.
 * 
 * @since 0.4.14
 * @author <a href="mailto:xnnyygn@gmail.com">xnnyygn</a>
 */
public class AnalyzeKannjiYomi {

	/**
	 * Main.
	 * 
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
//		int argumentCount = args.length;
//		if (argumentCount < 1) {
//			showUsage();
//			return;
//		}
//		String filePath = args[0];
//		WordList wordList = parseWordListFile(filePath);
//		KannjiYomiAnalyzer analyzer = new KannjiYomiAnalyzer();
//		OutputStream kannjiYomiPairOutput = argumentCount >= 2 ? new FileOutputStream(
//				args[1]) : System.out;
//		OutputStream analysisFailureWordOutput = argumentCount >= 3 ? new FileOutputStream(
//				args[2]) : System.out;
//		BufferedWriter kannjiYomiPairWriter = new BufferedWriter(
//				new OutputStreamWriter(kannjiYomiPairOutput, "UTF-8"));
//		kannjiYomiPairWriter.write("A B C D");
//		kannjiYomiPairWriter.newLine();
//		BufferedWriter analysisFailureWordWriter = new BufferedWriter(
//				new OutputStreamWriter(analysisFailureWordOutput, "UTF-8"));
//		analysisFailureWordWriter.write("A B");
//		analysisFailureWordWriter.newLine();
//		int successCount = 0;
//		int analysisFailureCount = 0;
//		int noKannjiCount = 0;
//		Map<KannjiYomiPair, List<String>> kannjiWordMap = new TreeMap<KannjiYomiPair, List<String>>(
//				new KannjiYomiPairComparator());
//		for (int i = 0; i < wordList.getWordCount(); i++) {
//			Word word = wordList.getWord(i);
//			String rawKannji = word.get(0);
//			String rawYomi = word.get(1);
//			// System.out.println("Analyze " + rawKannji + ", " + rawYomi);
//			KannjiYomiDetectResult result = analyzer.analyze(rawKannji,
//					rawYomi);
//			if (result.isSuccess()) {
//				for (KannjiYomiPair pair : result) {
//					if (kannjiWordMap.containsKey(pair)) {
//						List<String> words = kannjiWordMap.get(pair);
//						words.add(result.getKannjiWithYomi());
//					} else {
//						List<String> words = new ArrayList<String>();
//						words.add(result.getKannjiWithYomi());
//						kannjiWordMap.put(pair, words);
//					}
//					successCount++;
//				}
//			} else {
//				if (result.getErrorCode() != KannjiYomiDetectResult.ERROR_CODE_NO_KANNJI) {
//					analysisFailureWordWriter.write(rawKannji + "\t" + rawYomi);
//					analysisFailureWordWriter.newLine();
//					analysisFailureCount++;
//				} else {
//					noKannjiCount++;
//				}
//			}
//		}
//		for (Map.Entry<KannjiYomiPair, List<String>> entry : kannjiWordMap
//				.entrySet()) {
//			KannjiYomiPair pair = entry.getKey();
//			kannjiYomiPairWriter.write(pair.getKannji() + "\t" + pair.getYomi()
//					+ "\t" + Arrays.toString(entry.getValue().toArray()));
//			kannjiYomiPairWriter.newLine();
//		}
//		kannjiYomiPairWriter.flush();
//		if (argumentCount >= 2) {
//			kannjiYomiPairWriter.close();
//		}
//		analysisFailureWordWriter.flush();
//		if (argumentCount >= 3) {
//			analysisFailureWordWriter.close();
//		}
//		// System.out.println(kannjiWordMap.size());
//		// System.out.println(successCount);
//		System.out.println(analysisFailureCount);
//		// System.out.println(noKannjiCount);
	}

	/**
	 * Show usage.
	 */
	private static void showUsage() {
		System.out.println("usage <filePath>");
	}

	/**
	 * Parse word list from file.
	 * 
	 * @param filePath
	 * @return
	 * @throws UnsupportedFileExtensionException
	 * @throws WordListParseException
	 */
	private static WordList parseWordListFile(String filePath)
			throws UnsupportedFileExtensionException, WordListParseException {
		WordListParserFactory parserFactory = new WordListParserFactory();
		WordListParser parser = parserFactory.getParser(filePath);
		WordList wordList = parser.parse(filePath);
		return wordList;
	}

}
